package cn.com.fy.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.fy.baidu.tanslate.TestTranlate;
import cn.com.fy.pojo.Areas;
import cn.com.fy.pojo.Cities;
import cn.com.fy.pojo.PlaceNameFy;
import cn.com.fy.pojo.Provinces;
import cn.com.fy.service.AreasService;
import cn.com.fy.service.CitiesService;
import cn.com.fy.service.PlaceNameFyService;
import cn.com.fy.service.ProvincesService;

@Controller
@RequestMapping("fy")
public class PlaceNameFyController {

	@Autowired
	private PlaceNameFyService placeNameFyService ;
	@Autowired
	private ProvincesService provincesService;
	@Autowired
	private CitiesService citiesService;
	@Autowired
	private AreasService areasService;
	//27种语语言
	String[] tos = {"en","yue","wyw","jp","kor","fra","spa","th","ara"
			,"ru","pt","de","it","el","nl","pl","bul","est","dan","fin",
			"cs","rom","slo","swe","hu","cht","vie"}; 
	String[] ch = {"英语","粤语","文言文","日语","韩语","法语","西班牙语","泰语","阿拉伯语"
			,"俄语","葡萄牙语","德语","意大利语","希腊语","荷兰语","波兰语","保加利亚语","爱沙尼亚语","丹麦语","芬兰语",
			"捷克语","罗马尼亚语","斯洛文尼亚语","瑞典语","匈牙利语","繁体中文","越南语"};
	private List<HashMap<String, String>> getLanguages() {
		List<HashMap<String, String>> list = new ArrayList<>();
		for(int i = 0 ; i < tos.length ; i++) {
			HashMap<String, String> languages = new HashMap<String,String>();
			languages.put("en", tos[i]);
			languages.put("ch", ch[i]);
			list.add(languages);
		}
		return list;
	}
	@RequestMapping("/save")
	public Object savePlacenName() throws Exception {
		List<PlaceNameFy> placeNameFies = new ArrayList<PlaceNameFy>();
		for(int i = 0 ; i < 2 ; i++) {
			PlaceNameFy placeNameFy = new PlaceNameFy();
			placeNameFy.setName("北京");
			placeNameFy.setLanguages("英语");
			placeNameFy.setTranslate("beijing");
			placeNameFies.add(placeNameFy);
		}
		placeNameFyService.save(placeNameFies);

		return "freemarker/index";
	}

	@ResponseBody
	@RequestMapping(value = "/translate", method = RequestMethod.POST)
	public String findTranslate(String area,String language) {
		String resoult = getTranslate(area, language);
		System.out.println(area+language+resoult);
		return resoult;
	}
	private String getTranslate(String area,String language) {
		String resoult = "";
		List<PlaceNameFy> list = placeNameFyService.findTranslate(area, language);
		if (!list.isEmpty()) {
			resoult = list.get(0).getTranslate();
		}
		return resoult;
	}
	@PostMapping("/runTran")
	public Object hellowWord() throws UnsupportedEncodingException {
		

		TestTranlate tranlate = new TestTranlate();
		//省份
//		List<Provinces> provinces = provincesService.findAll();
		//市
//		List<Cities> provinces = citiesService.findAll();
		//
		List<Areas> provinces = areasService.findAll();
		try {
			for(int i = 0;i < provinces.size(); i++) {
				
				String area = "";
				List<PlaceNameFy> placeNameFies = new ArrayList<PlaceNameFy>();
				for(int j = 0; j < tos.length ; j++) {
//					area = provinces.get(i).getProvince();
//					area = provinces.get(i).getCity();
					area = provinces.get(i).getArea();
					PlaceNameFy placeNameFy = new PlaceNameFy();
					placeNameFy.setName(area);
					String translate = tranlate.getTranslate(area,tos[j]);
					placeNameFy.setTranslate(translate);
					placeNameFy.setLanguages(tos[j]);
					placeNameFies.add(placeNameFy);
//					placeNameFyService.savePlace(placeNameFy);
					
				}
				System.out.println(area+"翻译完成:"+placeNameFies.size());
				placeNameFyService.save(placeNameFies);
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("翻译完成");
		return "freemarker/index";
	}
	
	@RequestMapping("index")
	public String index(ModelMap map) {
		map.addAttribute("languages", getLanguages());
		return "freemarker/index";
	}
}
